/*
 @description: 后端管理员增删查改
*/
'use strict';
const Controller = require('egg').Controller;
const utility = require("utility")

class AdminuserController extends Controller {
    // 查询
    async index() { //搜索
        const {
            ctx,
            app
        } = this;
        // page 分页默认为第一页，number 每页5条数据 ,keyname后台传进来的
        const {
            keyname,
            page = 1,
            number = 5
        } = ctx.request.query;
        // 如果page=2,则offset = 2,翻页为第二页,这个page是根据后台传进来的
        const offset = (page - 1) * number;
        let data, count; //data数据 ， count全部数据的数量
        const power = await app.mysql.query(`select jurisdiction from adminuser where name like '${keyname}'`);
        console.log(power);
        if (keyname && page) {
            // 查询数据库管理员表的字段,根据名字模糊查询,
            data = await app.mysql.query(`select * from adminuser where name like '%${keyname}%' limit ${offset},${number}`);
            // like '%${keyname}%' limit ${offset},${number}  模糊查询(keyname传进来的名字) ，然后默认查询出来第一页 显示多少条数据 
            // 查询所有的表的数据条数count 
            count = await app.mysql.query(`select count(*) as count from adminuser where name like '%${keyname}%'`);
        } else if (keyname) {
            data = await app.mysql.query(`select * from adminuser where name ='${keyname}' or name  like '%${keyname}%' limit ${offset},${number}`);
            count = await app.mysql.query(`select count(*) as count from adminuser where name like '%${keyname}%'`);
        } else {
            data = await app.mysql.query(`select * from adminuser limit ${offset},${number}`);
            count = await app.mysql.query(`select count(*) as count from adminuser`);
        }
       if (count[0].count != 0) {
            ctx.body = {
                code: 0,
                msg: "查找成功",
                data,
                count: count[0].count,
            }
        } else {
            ctx.body = {
                code: 4004,
                msg: "查找失败"
            }
        }
    }
    // 这个查询是根据id查询
    async show() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        const result = await app.mysql.get('adminuser', {
            ID: id
        })
        if (result) {
            ctx.body = {
                code: 0,
                msg: '查找成功',
                data: result
            };
        } else {
            ctx.body = {
                code: 4007,
                msg: "查找失败"
            }
        }
    }

    //添加
    async create() {
        const {
            ctx,
            app
        } = this;
        // 从后台传过来的
        const {
            name,
            password,
            headPortrait,
            jurisdiction = 2
        } = ctx.request.body
        // jurisdiction=2;
        // 如果名字和密码同时传值过来就执行
        // console.log(jurisdiction);
        if (name && password && jurisdiction ) {
            // 查找数据库表的所有名字,
            const result = await app.mysql.query(`select name from adminuser`)
            // 如果查找到的名字一样,则不能添加同名的管理员
            const result1 = result.filter(item => item.name == name)
            if (result1.length !== 0) {
                ctx.body = {
                    code: 4006,
                    msg: "添加失败,已有此管理员",
                };
            } else {
                // 加密
                const confPwd = utility.md5(password);
                const row = {
                    name,
                    password: confPwd,
                    headPortrait,
                }
                // 插入数据库的 adminuser表
                const data = await app.mysql.insert('adminuser', row);
                ctx.body = {
                    code: 0,
                    msg: "添加成功",
                    data:row
                };
            }
        }else{
            ctx.body = {
                code:4005,
                msg:'缺少默认参数'
            }
        }
    }
    //修改
    async update() { //put
        const {
            ctx,
            app
        } = this;
        const {
            name,
            password = 0,
            headPortrait
        } = ctx.request.body
        // 获取后台选中的id
        const {
            id
        } = ctx.params;
        const row = {
            name: name,
            password: utility.md5(password),
            headPortrait:headPortrait         
        };
        const options = {
            // 将要修改的数据,放到获取的id那一列数据
            where: {
                id: +id
            }
        };
        // 修改到 adminuser的表
        const result = await app.mysql.update('adminuser', row, options);
        if (result) {
            ctx.body = {
                code: 0,
                msg: "编辑成功",
            };
        } else {
            ctx.body = {
                code: 4008,
                msg: "编辑失败",
            };
        }
    }
    // 删除
    async destroy() { //DELETE
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        const result = await app.mysql.delete(`adminuser`, {
            id: +id
        })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
            };
        } else {
            ctx.body = {
                code: 4007,
                msg: "删除失败",
            };
        }
    }
}
module.exports = AdminuserController;